//
// Created by 86151 on 2024/6/7.
//

#include "ultrasonic.h"
#include "cmsis_os.h"
ultrasonic_ ultrasonicOne;
ultrasonic_ ultrasonicSecond;

void sendUltrasonic(uint8_t flag) {
    if (flag == 0x11) {
        ultrasonicOne.measureSign[0] = 0x55;
        ultrasonicOne.measureSign[1] = 0xaa;
        ultrasonicOne.measureSign[2] = 0x11;
        ultrasonicOne.measureSign[3] = 0x00;
        ultrasonicOne.measureSign[4] = 0x01;
        ultrasonicOne.measureSign[5] = 0;

        ultrasonicOne.measureData[0] = 0x55;
        ultrasonicOne.measureData[1] = 0xaa;
        ultrasonicOne.measureData[2] = 0x11;
        ultrasonicOne.measureData[3] = 0x00;
        ultrasonicOne.measureData[4] = 0x02;
        ultrasonicOne.measureData[5] = 0;
        for (int i = 0; i < 5; i++) {
            ultrasonicOne.measureSign[5] += ultrasonicOne.measureSign[i];
        }
        rs485_2_send_msg(ultrasonicOne.measureSign, 6);
        osDelay(10);
        for(int i=0;i<5;i++){
            ultrasonicOne.measureData[5] += ultrasonicOne.measureData[i];
        }
        rs485_2_send_msg(ultrasonicOne.measureData, 6);
        osDelay(10);
    } else if (flag == 0x12) {
        ultrasonicSecond.measureSign[0] = 0x55;
        ultrasonicSecond.measureSign[1] = 0xaa;
        ultrasonicSecond.measureSign[2] = 0x12;
        ultrasonicSecond.measureSign[3] = 0x00;
        ultrasonicSecond.measureSign[4] = 0x01;
        ultrasonicSecond.measureSign[5] = 0x00;

        ultrasonicSecond.measureData[0] = 0x55;
        ultrasonicSecond.measureData[1] = 0xaa;
        ultrasonicSecond.measureData[2] = 0x12;
        ultrasonicSecond.measureData[3] = 0x00;
        ultrasonicSecond.measureData[4] = 0x02;
        ultrasonicSecond.measureData[5] = 0x00;
        for (int i = 0; i < 5; i++) {
            ultrasonicSecond.measureSign[5] += ultrasonicSecond.measureSign[i];
        }
        rs485_2_send_msg(ultrasonicSecond.measureSign, 6);
        osDelay(10);
        for(int i=0;i<5;i++){
            ultrasonicSecond.measureData[5] += ultrasonicSecond.measureData[i];
        }
        rs485_2_send_msg(ultrasonicSecond.measureData, 6);
        osDelay(10);
    }
}
void RxCpltCallbackUltrasonic(UART_HandleTypeDef *huart) {
//    printf("**********\n");
    uint8_t SUM=0;
        for (int i = 0; i < RS485_BUFFER_SIZE_2 - 1; i++) {
            SUM += rs485_rx_Buffer_2[i];
        }
        if (SUM == rs485_rx_Buffer_2[RS485_BUFFER_SIZE_2 - 1]) {
            if(rs485_rx_Buffer_2[2]==0x11){
                ultrasonicOne.distance=rs485_rx_Buffer_2[5] << 8 | rs485_rx_Buffer_2[6];
            //    printf("0x11:%d\n", rs485_rx_Buffer_2[5] << 8 | rs485_rx_Buffer_2[6]);
            }
            else if(rs485_rx_Buffer_2[2]==0x12){
                ultrasonicSecond.distance=rs485_rx_Buffer_2[5] << 8 | rs485_rx_Buffer_2[6];
            //    printf("0x12:%d\n", rs485_rx_Buffer_2[5] << 8 | rs485_rx_Buffer_2[6]);
            }
            // printf("************************\n");
        }
    //memset(rs485_rx_Buffer_2, 0, sizeof rs485_rx_Buffer_2);
    HAL_UART_Receive_IT(&RS485_2_UART, rs485_rx_Buffer_2, RS485_BUFFER_SIZE_2);
}

